const app = getApp()
const api = require('../fetch/api')
const {
  navBarHeight,
  statusBarHeight
} = app.globalData
const {
  baseURL,
  ip
} = require('../fetch/http')

Component({
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 3
        })
      }
      const pages = getCurrentPages()
      const currentPage = pages[pages.length - 1]
      let id = currentPage.options.id
      if (id) {
        wx.setStorageSync('recommend', id)
      }


      if (wx.getStorageSync('token')) {
        this.getUserinfo()
        this.orderNum()
      }else{
      wx.navigateTo({
        url: '/pages/my/login/login',
      })
      }
    },
  },
  lifetimes: {
   
  },
  data: {
    ip: ip,
    baseURL: baseURL,
    statusBarHeight: statusBarHeight,
    bottomHeight: navBarHeight - statusBarHeight,
    nickname: '',
    headimg: '',
    team:""
  },
  methods: {
    async orderNum() {
      let res = await api.orderNum()
      // console.log(res);
      let {
        noPay,
        noSignFor,
        noDelivery,
        finish
      } = res.data
      this.setData({
        noPay,
        noSignFor,
        noDelivery,
        finish
      })
    },
    async getUserinfo() {
      let res = await api.userinfo()
      
      let {
        data: infoRes
      } = await api.getFinancialInfo()
  
      let {
        nickname,
        headimg,
        id,
        vip,
        vipDaoqi,
        team,
        balance
      } = res.data
      this.setData({
        nickname,
        headimg,
        totalCommission: infoRes.totalCommission,
        balance: infoRes.balance,
        id,
        vip,
        vipDaoqi,
        team,
        
      })
      app.globalData.userinfo = res.data
    },
    car() {
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/car/index',
        })
      })
    },
    promotion() {
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/promotion/index',
        })
      })
    },
    earnings() {
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/earnings/index',
        })
      })
    },
    vippower(){
      wx.navigateTo({
        url: '/pages/my/vipPower/index?id=5',
      })
    },
    rule() {
      wx.navigateTo({
        url: '/pages/my/rule/index',
      })
    },
    
    team() {
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/team/index'
        })
      })
    },
    address() {
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/address/index',
        })
      })
    },
    about(){
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/vipPower/index?id=6',
        })
      })
    },
    // 全部
    orders(e) {
      const {
        index
      } = app.dataSet(e)
      // console.log(index);
      app.routerTo(() => {
        wx.navigateTo({
          url: '/pages/my/orders/index?index=' + index
        })
      })
    },
    accountMannge(){
       wx.navigateTo({
         url: '/pages/accountManage/index/index',
       })
    },
    onShareAppMessage: function () {
      if (!wx.getStorageSync('token')) {
        app.toast('请登录')
        return false
      }

      return {
        title: '天喜鑫宝',
        imageUrl: this.data.baseURL + '/img/share_img_2.png',
        path: '/index/index4?id=' + wx.getStorageSync('id')
      }
    }
  }
})